package com.example.querydsl.controller;

import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;




/**
 * @Author Anita
 * @Date 2017/12/20 14:11
 */
public class Inquirer {
    //查询条件集合
    private List<BooleanExpression> expressions;
    public Inquirer(){
        this.expressions = new ArrayList<>();
    }

    /**
     * 添加查询条件到Query内的查询集合内
     * @param expression
     * @return
     */
    public Inquirer putExpression(BooleanExpression expression){
        expressions.add(expression);
        return this;
    }

    /**
     * 构建出查询findAll函数使用的Predicate接口查询对象
     * @return
     */
    public Predicate buildQuery(){
        BooleanExpression be = null;
        for(int i=0;i<expressions.size();i++){
            if(i==0)
                be = expressions.get(0);
            else
                be = be.and(expressions.get(i));
        }

        return be;
    }

    public <T> List<T> iteratorToList(Iterable<T> iterable){
        List<T> returnList = new ArrayList<>();
        Iterator<T> iterator = iterable.iterator();
        while(iterator.hasNext()){
            returnList.add(iterator.next());
        }
        return returnList;
    }



}
